﻿Imports Microsoft.Xna.Framework
Imports System.Reflection
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Text
Imports Runa.Foundation.Collections
Imports Runa.Resources.ContentManagers
Imports Runa.UI.Controls

Namespace Global.Runa.UI.Styles

    <DefaultMember("Item"), XmlRoot("Style")> _
    Public Class Style

        Public Name As String
        Public Propertys As Dictionary(Of String, Object)

        Protected Game As Game
        Protected ReadOnly Property _ResourceContentManager() As ResourceStreamContentManager
            Get
                Return New ResourceStreamContentManager(Game.Services, GetType(UIElementBase).Assembly.GetName().Name, GetType(UIElementBase).Assembly)
            End Get
        End Property

        Public Sub New()
            Me.Propertys = New Dictionary(Of String, Object)
        End Sub

        Public Sub New(ByVal reader As XmlReader)
            Me.New()
            Me.Load(reader)
        End Sub

        Public Sub New(ByVal g As Game)
            Me.New()
            Me.Game = g
        End Sub

        Public Sub New(ByVal StyleName As String, ByVal g As Game)
            Me.New()
            Me.Name = StyleName
            Me.Game = g
        End Sub

        Protected Overridable Sub Load(ByVal xml As XmlReader)
            Dim xs As New XmlSerializer(GetType(SerializableDictionary(Of String, IXmlSerializable)))
            Dim propertys As SerializableDictionary(Of String, Object) = TryCast(xs.Deserialize(xml), SerializableDictionary(Of String, Object))
            If (Not propertys Is Nothing) Then
                Me.Propertys = propertys
            End If
        End Sub

        Public Overridable Function Save() As StringBuilder
            Dim xml As New StringBuilder
            Dim xw As XmlWriter = XmlWriter.Create(xml)
            Dim xs = New XmlSerializer(Me.Propertys.GetType)
            xs.Serialize(xw, Me.Propertys)
            Return xml
        End Function

        Default Public Property Item(ByVal name As String) As Object
            Get
                If Me.Propertys.ContainsKey(name) Then
                    Return Me.Propertys.Item(name)
                End If
                Return Nothing
            End Get
            Set(ByVal value As Object)
                If Me.Propertys.ContainsKey(name) Then
                    Me.Propertys.Item(name) = value
                Else
                    Me.Propertys.Add(name, value)
                End If
            End Set
        End Property

    End Class

End Namespace